package smecta.client.print.util;

import javafx.scene.control.TableCell;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.util.StringConverter;
import javafx.scene.control.TableColumn;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;

public class CustomTextFieldTableCell<S, T> extends TableCell<S, T> {
    private final TextField textField;
    private final StringConverter<T> converter;

    private String start;
    private String end;

    public CustomTextFieldTableCell(StringConverter<T> converter) {
        this.converter = converter;
        this.textField = new TextField();
        this.textField.setOnAction(evt -> commitEdit(converter.fromString(textField.getText())));
        this.textField.focusedProperty().addListener(new ChangeListener<Boolean>() {
            @Override
            public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                if (!newValue) {
                    commitEdit(converter.fromString(textField.getText()));
                }
            }
        });
        this.textField.setOnKeyPressed(t -> {
            if (t.getCode() == KeyCode.ESCAPE) {
                cancelEdit();
            }
        });
    }

    public CustomTextFieldTableCell(StringConverter<T> converter,String start,String end) {
        this.start = start;
        this.end = end;
        this.converter = converter;
        this.textField = new TextField();
        this.textField.setOnAction(evt -> commitEdit(converter.fromString(textField.getText())));
        this.textField.focusedProperty().addListener(new ChangeListener<Boolean>() {
            @Override
            public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                if (!newValue) {
                    commitEdit(converter.fromString(textField.getText()));
                }
            }
        });
        this.textField.setOnKeyPressed(t -> {
            if (t.getCode() == KeyCode.ESCAPE) {
                cancelEdit();
            }
        });
    }

    @Override
    public void startEdit() {
        if (!isEmpty()) {
            super.startEdit();
            textField.setText(converter.toString(getItem()));
            setText(null);
            setGraphic(textField);
            textField.selectAll();
            textField.requestFocus();
        }
    }

    @Override
    public void cancelEdit() {
        super.cancelEdit();
        setText(converter.toString(getItem()));
        setGraphic(null);
    }

    @Override
    public void updateItem(T item, boolean empty) {
        super.updateItem(item, empty);
        System.out.println("========="+getTableColumn().getText());
        if (empty) {
            setText(null);
            setGraphic(null);
            setStyle("");
        } else {
            if (isEditing()) {
                textField.setText(converter.toString(getItem()));
                setText(null);
                setGraphic(textField);
                setStyle("");
            } else {
                setText(converter.toString(getItem()));
                String text = getTableColumn().getText();
                String value = converter.toString(getItem());
                if ("筒数".equals(text) && StringUtils.isNotBlank(value)){
                    if (StringUtils.isBlank(start) || StringUtils.isBlank(end)){
                        setStyle("");
                    }else {
                        int i = Integer.parseInt(start);
                        int i1 = Integer.parseInt(end);
                        int canNum = Integer.parseInt(value);
                        if (canNum < i || canNum > i1){
                            setStyle("-fx-background-color: red");
                        }else {
                            setStyle("");
                        }
                    }
                }else if ("净重".equals(text) && StringUtils.isNotBlank(value)){
                    if (StringUtils.isBlank(start) || StringUtils.isBlank(end)){
                        setStyle("");
                    }else {
                        BigDecimal bigDecimal = new BigDecimal(start);
                        BigDecimal bigDecimal1 = new BigDecimal(end);
                        BigDecimal bigDecimal2 = new BigDecimal(value);
                        if (bigDecimal2.compareTo(bigDecimal) == -1 || bigDecimal2.compareTo(bigDecimal1) == 1){
                            setStyle("-fx-background-color: red");
                        }else {
                            setStyle("");
                        }
                    }
                }
                setGraphic(null);
            }
        }
    }
}
